using System.Collections.Generic;
using System.Web.Routing;
using Nop.Core.Domain.Cms;
using Nop.Core.Plugins;
using Nop.Services.Cms;
using Nop.Services.Configuration;
using Nop.Services.Localization;

namespace Nop.Plugin.Widgets.ExternalScripts
{
    /// <summary>
    /// Live person provider
    /// </summary>
    public class ExternalScriptsPlugin : BasePlugin, IWidgetPlugin
    {
        private readonly ISettingService _settingService;
        private readonly ExternalScriptsSettings _externalScriptsSettings;

        public ExternalScriptsPlugin(ISettingService settingService,
            ExternalScriptsSettings ExternalScriptsSettings)
        {
            this._settingService = settingService;
            this._externalScriptsSettings = ExternalScriptsSettings;
        }

        /// <summary>
        /// Gets widget zones where this widget should be rendered
        /// </summary>
        /// <returns>Widget zones</returns>
        public IList<string> GetWidgetZones()
        {
            return !string.IsNullOrWhiteSpace(_externalScriptsSettings.WidgetZone)
                       ? new List<string>() { _externalScriptsSettings.WidgetZone }
                       : new List<string>() { "body_end_html_tag_before" };
        }

        /// <summary>
        /// Gets a route for provider configuration
        /// </summary>
        /// <param name="actionName">Action name</param>
        /// <param name="controllerName">Controller name</param>
        /// <param name="routeValues">Route values</param>
        public void GetConfigurationRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues)
        {
            actionName = "Configure";
            controllerName = "ExternalScripts";
            routeValues = new RouteValueDictionary() { { "Namespaces", "Nop.Plugin.Widgets.ExternalScripts.Controllers" }, { "area", null } };
        }

        /// <summary>
        /// Gets a route for displaying widget
        /// </summary>
        /// <param name="widgetZone">Widget zone where it's displayed</param>
        /// <param name="actionName">Action name</param>
        /// <param name="controllerName">Controller name</param>
        /// <param name="routeValues">Route values</param>
        public void GetDisplayWidgetRoute(string widgetZone, out string actionName, out string controllerName, out RouteValueDictionary routeValues)
        {
            actionName = "PublicInfo";
            controllerName = "ExternalScripts";
            routeValues = new RouteValueDictionary()
            {
                {"Namespaces", "Nop.Plugin.Widgets.ExternalScripts.Controllers"},
                {"area", null},
                {"widgetZone", widgetZone}
            };
        }

        /// <summary>
        /// Install plugin
        /// </summary>
        public override void Install()
        {
            var settings = new ExternalScriptsSettings()
            {
                
            
            };
            _settingService.SaveSetting(settings);

            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ExternalScripts.ScriptSystemName", "Script SystemName");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ExternalScripts.ScriptDescription", "Script description");

            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ExternalScripts.DefaultScript", "Default script");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ExternalScripts.DefaultScript.Hint", "Enter the default script here. {VALUE}, {FREETOKEN} will be dynamically replaced. http will also be automatically replaced with https when necessary.");

            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ExternalScripts.CheckoutCompletedScript", "Specific script for checkout completed page, with {ORDER_ITEMS_SCRIPT} line");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ExternalScripts.CheckoutCompletedScript.Hint", "Enter the script for checkout completed page. {ORDERID}, {SITE}, {SUBTOTALEXCLTAX}, ect. will be dynamically replaced. Read documentation for a complete token list.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ExternalScripts.CheckoutCompletedDetailScript", "Order detail script for {ORDER_ITEMS_SCRIPT} part");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ExternalScripts.CheckoutCompletedDetailScript.Hint", "Enter the script for {ORDER_ITEMS_SCRIPT} part here. {ORDERID}, {PRODUCTSKU}, {PRODUCTNAME}, ect. will be dynamically replaced. Read documentation for a complete token list.");
            
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ExternalScripts.ProductScript", "Specific script for products pages");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ExternalScripts.ProductScript.Hint", "Specific script for products pages");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ExternalScripts.CategoryScript", "Specific script for categories pages");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ExternalScripts.CategoryScript.Hint", "Specific script for categories pages");
           
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ExternalScripts.ProductIdsAndFreeTokens", "List of product ids and optional free tokens");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ExternalScripts.ProductIdsAndFreeTokens.Hint", "List(;) of lists(,) of  product ids and (:) and free token. Read documentation for more information.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ExternalScripts.CategoryIdsAndFreeTokens", "List of categories ids and optional free tokens");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ExternalScripts.CategoryIdsAndFreeTokens.Hint", "List(;) of lists(,) of  category ids and (:) and optional free token. Read documentation for more information.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ExternalScripts.ContollersAndFreeTokens", "List of controllers/actions and free tokens");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.ExternalScripts.ContollersAndFreeTokens.Hint", "List(;) of controllers/actions and (:) and optional free token. Read documentation for more information.");

            base.Install();
        }

        /// <summary>
        /// Uninstall plugin
        /// </summary>
        public override void Uninstall()
        {
            //locales
            this.DeletePluginLocaleResource("Plugins.Widgets.ExternalScripts.ScriptSystemName");
            this.DeletePluginLocaleResource("Plugins.Widgets.ExternalScripts.ScriptDescription");
            this.DeletePluginLocaleResource("Plugins.Widgets.ExternalScripts.DefaultScript");
            this.DeletePluginLocaleResource("Plugins.Widgets.ExternalScripts.DefaultScript.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.ExternalScripts.CheckoutCompletedScript");
            this.DeletePluginLocaleResource("Plugins.Widgets.ExternalScripts.CheckoutCompletedScript.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.ExternalScripts.CheckoutCompletedDetailScript");
            this.DeletePluginLocaleResource("Plugins.Widgets.ExternalScripts.CheckoutCompletedDetailScript.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.ExternalScripts.ProductScript");
            this.DeletePluginLocaleResource("Plugins.Widgets.ExternalScripts.ProductScript.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.ExternalScripts.CategoryScript");
            this.DeletePluginLocaleResource("Plugins.Widgets.ExternalScripts.CategoryScript.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.ExternalScripts.ProductIdsAndFreeTokens");
            this.DeletePluginLocaleResource("Plugins.Widgets.ExternalScripts.ProductIdsAndFreeTokens.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.ExternalScripts.CategoryIdsAndFreeTokens");
            this.DeletePluginLocaleResource("Plugins.Widgets.ExternalScripts.CategoryIdsAndFreeTokens.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.ExternalScripts.ContollersAndFreeTokens");
            this.DeletePluginLocaleResource("Plugins.Widgets.ExternalScripts.ContollersAndFreeTokens.Hint");

            base.Uninstall();
        }
    }
}
